<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/15
 */

namespace App\Http\Controllers;

use App\Filters\AdsFilter;
use App\Http\Requests\AdsRequest;
use App\Http\Resources\AdsCollection;
use App\Http\Resources\AdsResource;
use App\Models\Ads;
use App\Models\AdsSpace;
use App\Services\RedisService;
use Illuminate\Support\Facades\AuthAuth;
use Illuminate\Support\Facades\DB;

/**
 * 广告模块
 * Class AdsController
 * @package App\Http\Controllers
 */
class AdsController extends Controller
{

    /**
     * 列表
     * @param AdsSpace $ads_space
     * @param AdsFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(AdsSpace $ads_space, AdsFilter $filter)
    {
        $resource = Ads::filter($filter)->where('space_id', $ads_space->id)->paginate($this->pageSize());;
        $result = Adscollection::make($resource)->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param AdsRequest $request
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(AdsRequest $request, AdsSpace $ads_space)
    {
        $data = $request->validate('store');
        $ads = new Ads($data);
        if (!$ads->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param AdsSpace $ads_space
     * @param Ads $ad
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(AdsSpace $ads_space, Ads $ad)
    {
        $model = Ads::where(['id' => $ad->id, 'space_id' => $ads_space->id])->first();
        $result = AdsResource::make($model)->except(['created_at', 'updated_at'])->toArray(null);
        if (!$result) return $this->error(__('common.http_not_found'));
        return $this->success('', $result);
    }

    /**
     * 修改
     * @param AdsRequest $request
     * @param AdsSpace $ads_space
     * @param Ads $ad
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(AdsRequest $request, AdsSpace $ads_space, Ads $ad)
    {
        $data = $request->validate('update');
        $result = Ads::where(['id' => $ad->id, 'space_id' => $ads_space->id])->first();
        if (!$result || !$result->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param AdsSpace $ads_space
     * @param $id // 1或'1,2'
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(AdsSpace $ads_space, $id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            Ads::where('space_id', $ads_space->id)->whereIn('id', $between)->delete();
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param AdsRequest $request
     * @param AdsSpace $ads_space
     * @param Ads $ad
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(AdsRequest $request, AdsSpace $ads_space, Ads $ad)
    {
        $data = $request->validate('updateStatus');
        $result = Ads::where(['id' => $ad->id, 'space_id' => $ads_space->id])->first();
        if (!$result || !$result->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param AdsRequest $request
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(AdsRequest $request, AdsSpace $ads_space)
    {
        $data = $request->validate('updateSort');
        $ads = new Ads();
        $result = $ads->batchUpdate($data['sort'], 'id', ' space_id = ? ', [$ads_space->id]);
        if (!$result) return $this->errorUpdated();
        return $this->successUpdated();
    }

}
